package com.youlu.campus.service.security.impl;

import com.youlu.campus.base.exception.BusinessException;
import com.youlu.campus.entity.SecurityIpLimit;
import com.youlu.campus.entity.SmsSendRecord;
import com.youlu.campus.service.security.SecurityIpLimitService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Objects;


@Slf4j
@Service
public class SecurityIpLimitServiceImpl implements SecurityIpLimitService {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public boolean exist(String ip) {
        log.info(":>>> 开始查询IP是否在限制中:{}", ip);
        Query query = new Query().addCriteria(Criteria.where("ip").is(ip).and("status").is(Boolean.TRUE));
        boolean exist = mongoTemplate.exists(query, SecurityIpLimit.class);
        log.info(":>>> IP:{} 在黑名单中:{}", ip, exist);
        return exist;
    }

    @Override
    public boolean create(SecurityIpLimit req) {
        req.setCreatedTime(new Date());
        mongoTemplate.insert(req);
        return true;
    }

    @Override
    public boolean incre(String ip, Long num) {
        Query query = new Query().addCriteria(Criteria.where("ip").is(ip));
        Update update = new Update();
        update.inc("num", num);
        update.set("updatedTime", new Date());
        mongoTemplate.updateFirst(query, update, SecurityIpLimit.class);
        return true;
    }

    @Override
    public boolean limited(String ip) {
        log.info(":>>> 开始判断IP是否在黑名单中:{}", ip);
        SecurityIpLimit s = find(ip);
        if (Objects.isNull(s)) {
            return false;
        } else {
            if (s.getNum().intValue() > 50) {
                log.info(":>>> IP:{} 发送验证码超过50次,在黑名单中", ip);
                return true;
            }
        }
        return false;
    }

    @Override
    public SecurityIpLimit find(String ip) {
        Query query = new Query().addCriteria(Criteria.where("ip").is(ip));
        SecurityIpLimit securityIpLimit = mongoTemplate.findOne(query, SecurityIpLimit.class);
        return securityIpLimit;
    }

    @Override
    public boolean limited(String userId, String mobile) {
        log.info(":><> userId:{},mobile:{}", userId, mobile);
        if (StringUtils.isBlank(mobile)) {
            log.error(":>>> 手机号为空");
            throw new BusinessException("或手机号为空");
        }
        Query query = new Query();
        query.addCriteria(Criteria.where("mobile").is(mobile));
        if (StringUtils.isNotBlank(userId)) {
            query.addCriteria(Criteria.where("userId").is(userId));
        }
        Long count = mongoTemplate.count(query, SmsSendRecord.class);
        if (count.intValue() >= 50) {
            log.error(":>>> 用户:{},手机号:{} 发送超过50次", userId, mobile);
            return true;
        }
        return false;
    }
}
